package com.tjsinfo.tjsoa.workflow;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipInputStream;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.Node;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.taskmgmt.exe.TaskMgmtInstance;

import com.tjsinfo.tjsoa.message.TjsMessageIm;

public class WorkFlowTemplate {
	 static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance(); 
	 static private JbpmContext jbpmContext;
	public static void main(String[] args) {
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		workFlowTemplate.testDeployProcessDefinition("D:\\workspace\\RQOA\\Design\\党委发文\\");
		//workFlowTemplate.testDeployProcessDefinition("D:\\Program\\Java\\workspace\\ZOA\\Design\\docOri\\");
		//workFlowTemplate.creatProcessInstance("var1", "back", "docCerCopy");
		//workFlowTemplate.changTaskActor(517l, "138");
	}
	   public void testDeployProcessDefinition() {
		   
	    	ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("xingzhengsenddoc/processdefinition.xml");
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    	jbpmContext.deployProcessDefinition(processDefinition);

	    	} finally {
	    	jbpmContext.close();
	    	}
	    	
	    }
	   /**j
	    * xml类型流程发布
	    * @param path
	    */
	   public void testDeployProcessDefinition(String path) {
		    InputStream inputStream = null;
			try {
				inputStream = new FileInputStream(path+"processdefinition.xml");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(inputStream);
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    	jbpmContext.deployProcessDefinition(processDefinition);

	    	} finally {
	    	jbpmContext.close();
	    	}
	    	
	    }
	   
	   /**
	    * xml类型流程发布
	    * @param path
	    */
	   public void testDeployProcessDefinition(String path,String filename) {
		    InputStream inputStream = null;
			try {
				inputStream = new FileInputStream(path+filename);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(inputStream);
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    	jbpmContext.deployProcessDefinition(processDefinition);

	    	} finally {
	    	jbpmContext.close();
	    	}
	    	
	    }
	   /**
	    * xml类型流程发布
	    * @param 流程文件和路径

	    */
	   public void processDefinitionDeploy(String processfilename) {
		    InputStream inputStream = null;
			try {
				inputStream = new FileInputStream(processfilename);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(inputStream);
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    	jbpmContext.deployProcessDefinition(processDefinition);

	    	} finally {
	    	jbpmContext.close();
	    	}
	    	
	    }
	   /**
	    * zip类型流程发布
	    * @param path
	    */
	   public void testDeployProcessDefinitionZip(String path) {
		    System.out.println(path+"processdefinition.zip");
		    InputStream inputStream = null;
			try {
				inputStream = new FileInputStream(path+"processdefinition.zip");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(new ZipInputStream(inputStream));
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    	jbpmContext.deployProcessDefinition(processDefinition);

	    	} finally {
	    	jbpmContext.close();
	    	}
	    	
	    }
	   
	    public long creatProcessInstance(String startusrname)
	    {
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				GraphSession graphSession =	jbpmContext.getGraphSession();
				ProcessDefinition processDefinition =	graphSession.findLatestProcessDefinition("shenpi");
				processInstance =	processDefinition.createProcessInstance();
				processInstance.getContextInstance().setVariable("startusername", startusrname);
				processInstance.signal();
				jbpmContext.save(processInstance);
				
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return processInstance.getId();
			
	    }
	    
	  
	    public long creatProcessInstance(String startvariablename,String startusrname,String processname)
	    {
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				GraphSession graphSession =	jbpmContext.getGraphSession();
				ProcessDefinition processDefinition =	graphSession.findLatestProcessDefinition(processname);
				processInstance =	processDefinition.createProcessInstance();
				processInstance.getContextInstance().setVariable(startvariablename, startusrname);
				processInstance.signal();
				jbpmContext.save(processInstance);
				
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return processInstance.getId();
			
	    }
	    /**
	     * 第一个节点决定方向

	     * @param startvariablename
	     * @param startusrname
	     * @param processname
	     * @param transitionName
	     * @return
	     */
	    public long creatProcessInstance(String startvariablename,String startusrname,String processname,String transitionName)
	    {
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				GraphSession graphSession =	jbpmContext.getGraphSession();
				ProcessDefinition processDefinition =	graphSession.findLatestProcessDefinition(processname);
				processInstance =	processDefinition.createProcessInstance();
				processInstance.getContextInstance().setVariable(startvariablename, startusrname);
				processInstance.signal(transitionName);
				jbpmContext.save(processInstance);
				
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return processInstance.getId();
			
	    }
	    /**
	     * 第一个节点决定方向

	     * @param startvariablename
	     * @param startusrname
	     * @param processname
	     * @param transitionName
	     * @return
	     */
	    public long creatProcessInstance(String startvariablename,List<String> startusrname,String processname,String transitionName)
	    {
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				GraphSession graphSession =	jbpmContext.getGraphSession();
				ProcessDefinition processDefinition =	graphSession.findLatestProcessDefinition(processname);
				processInstance =	processDefinition.createProcessInstance();
				processInstance.getContextInstance().setVariable(startvariablename, startusrname);
				processInstance.signal(transitionName);
				jbpmContext.save(processInstance);
				
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return processInstance.getId();
			
	    }
	    public void saveVariable(Long id,String variablename,String variablenvalue)
	    {	
	    	ProcessInstance processInstance = null;
	    	JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				processInstance.getContextInstance().setVariable(variablename, variablenvalue);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	finally
	    	{
	    		jbpmContext.close();
	    	}
	    	
	    }
	    public List selectTask(String username)
	    {
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	List list = null;
			try {
				list = jbpmContext.getTaskList(username);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
	    	return list;
	    }
	    
	    public List selectTask(String username,String proname)
	    {
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	List list = null;
	    	List list2 = new ArrayList();
			try {
				list = jbpmContext.getTaskList(username);
				for (Iterator iterator = list.iterator(); iterator.hasNext();) {
					TaskInstance taskInstance = (TaskInstance) iterator.next();
					if(taskInstance.getProcessInstance().getProcessDefinition().getName().equals(proname))
					{
						list2.add(taskInstance);
					}
					 
				}
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
	    	return list2;
	    }
	    
	    public  TaskInstance nextStep(Long id)
	    {
	    	TaskInstance instance = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
				 instance =	jbpmContext.getTaskInstanceForUpdate(id);
				 instance.getTask().getName();
				 instance.getProcessInstance().getId();
				return instance;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return instance;
	    }
	    public  void nextStep(Long id,String name)
	    {
	    	TaskInstance instance = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
				 instance =	jbpmContext.getTaskInstanceForUpdate(id);
				 instance.getTask().getName();
				 if (name == null) {
					nextStep(instance);
				} else {
					nextStep(instance,name);	
				}
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			
	    }
	    private  void nextStep(TaskInstance taskInstance)
	    {
	    	taskInstance.end();
	    }
	    private  void nextStep(TaskInstance taskInstance,String name)
	    {
	    	taskInstance.end(name);
	    }
	    public void setVariable(String name,Object object,Long id)
	    {
	    	ProcessInstance processInstance = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    		processInstance = jbpmContext.getProcessInstance(id);
				ContextInstance contextInstance =	processInstance.getContextInstance();
				contextInstance.setVariable(name, object);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			
	    }	
	    
	    public void setVariable1(String name,String object,Long id)
	    {
	    	ProcessInstance processInstance = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
	    		processInstance = jbpmContext.getProcessInstance(id);
				ContextInstance contextInstance =	processInstance.getContextInstance();
				contextInstance.setVariable(name, object);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			
	    }		    
	    
	    
	    public Object  getVariable(String name,Long id)
	    {
	    	Object object = null;
	    	ProcessInstance processInstance = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				ContextInstance contextInstance =	processInstance.getContextInstance();
				object =	contextInstance.getVariable(name);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return object;
	    }
	    
	    /**
	     * 查看当前位置
	     * @param 流程ID
	     * @return
	     */
	    public String selectNodeName(Long id)
	    {
	    	String nodename = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				nodename = processInstance.getRootToken().getNode().getName();
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return nodename;
	    }
	    
	    /**
	     * 流程向下运行
	     * @param 流程ID
	     * @return
	     */
	    public String signal(Long id)
	    {
	    	String nodename = null;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				processInstance.signal();
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return nodename;
	    }
	    
	    /**
	     * JBPM数据库查询HQL
	     */
	    public List selectList(String hql)
	    {
	    	List list = null;
	    	jbpmContext  = jbpmConfiguration.createJbpmContext();
	    	Session session =	jbpmContext.getSessionFactory().openSession();
	    	try {
				Query query =	session.createQuery(hql);
				list = query.list();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	finally
	    	{
	    		session.close();
	    		jbpmContext.close();
	    	}
	    	return list;
	    }
	    
	    /**
		 * 多人顺序追加人

		 * @param JBPM当前环节人

		 * @param 追加的人
		 * @param 分隔付

		 * @return
		 */
		public String addStr(String variable,String strarray,String fenge)
		{
			if (strarray!= null &&!"".equals(strarray)) {
				if ("".equals(variable)) {
					variable+=strarray;
				} 
				else if(variable.equals(strarray))
				{
					return "";
				}
				else {
					variable+=fenge+strarray;
				}
			} else {
				
				return variable;
			}
					
			return variable;
		}
		
		/**
		 * 多人顺序办理存入环节变量
		 * @param 环节变量名

		 * @param 环节实例ID
		 * @param 选入的他人可以为NULL
		 * @param 分隔符号
		 */
		public String addTaskVariable(String variablename,Long proid,String strarray,String fenge)
		{
			String var =	(String) getVariable(variablename, proid);
			 var =	addStr(var, strarray,fenge);
			if(!"".equals(var))
			{
				setVariable(variablename, var, proid);
			}
			
			return var;
		}
		
		/**
		 * 基于分隔符号取字符串第一个字符串
		 * @param str
		 * @param fenge
		 * @return
		 */
		public String firstStrArray(String str,String fenge)
		{
			if (str.indexOf(fenge) != -1) {
				str=str.substring(0,str.indexOf(fenge));
			} else {
				return str;
			}
			return str;
		}
		
		/**
		 * 流程退回功能

		 * @param 流程ID
		 * @param 环节名称
		 * @return
		 */
		public boolean backTask(Long flowid,Long flowinitionid, String nodename) {
					jbpmContext = jbpmConfiguration.createJbpmContext();
					Session hSession = jbpmContext.getSession();		
					// 获得要回退的流程实例ID
					ProcessInstance instance = null;
					instance = jbpmContext.getGraphSession().getProcessInstance(flowinitionid);
					ProcessDefinition definition = jbpmContext.getGraphSession().getProcessDefinition(instance.getProcessDefinition().getId());		
					if(instance==null)
					{
						return false;
					}
					// 获得要回退的任务

					Task task = definition.getTaskMgmtDefinition().getTask(nodename);
					System.out.println(task.getId());

					// 获得任务所属节点

					Node node = task.getTaskNode();

					// 获得要回退的任务实例

					TaskInstance backTaskInstance = null;

					Collection<TaskInstance> instances = instance.getTaskMgmtInstance()
							.getTaskInstances();
					for (TaskInstance taskInstance : instances) {
						// 获得要回退的任务实例

						if (taskInstance.getTask().getId() == task.getId()) {
							backTaskInstance = taskInstance;
							break;
						}
					}
					System.out.println("要回退的任务实例ID：" + backTaskInstance.getId());

					// 获得要回退的实例对应的令牌
					if (backTaskInstance == null) {
						return false;
					}
					Token token = backTaskInstance.getToken();
					System.out.println("要回退的令牌ID：" + token.getId());

					// 删除任务实例后所有的任务实例，包含其对应令牌，以及对应日志

					for (TaskInstance taskInstance : instances) {
						// 如果任务实例ID大于要退回的实例ID，则任务此任务实例在后面,应删除

						if (taskInstance.getId() > backTaskInstance.getId()) {
							Token deleteToken = taskInstance.getToken();
							// 删除对应的所有日志信息

							Query deleteLogQuery = hSession
									.createSQLQuery("delete from JBPM_LOG where 1=1 or TOKEN_ = ? or TASKINSTANCE_ = ?");
							deleteLogQuery.setParameter(0, taskInstance.getId());
							deleteLogQuery.setParameter(1, deleteToken.getId());
							deleteLogQuery.executeUpdate();

							// 删除任务实例
							Query deleteTaskInstanceQuery = hSession
									.createSQLQuery("delete from JBPM_TASKINSTANCE where ID_ = ?");
							deleteTaskInstanceQuery.setParameter(0, taskInstance.getId());
							deleteTaskInstanceQuery.executeUpdate();

							// 删除对应令牌
							Query deleteTokenQuery = hSession
									.createSQLQuery("delete from JBPM_TOKEN where ID_ = ?");
							deleteTokenQuery.setParameter(0, deleteToken.getId());
							deleteTokenQuery.executeUpdate();

						}

					}

					// 还原令牌信息
					Query updateTokenQuery = hSession
							.createSQLQuery("update JBPM_TOKEN set END_ = null,NODE_ = ? where ID_ = ?");
					updateTokenQuery.setParameter(0, node.getId());
					updateTokenQuery.setParameter(1, token.getId());
					updateTokenQuery.executeUpdate();

					// 还原实例信息
					Query updateTaskInstanceQuery = hSession
							.createSQLQuery("update JBPM_TASKINSTANCE set END_=null,ISOPEN_=1,ISSIGNALLING_=1 where ID_ = ?");
					updateTaskInstanceQuery.setParameter(0, backTaskInstance.getId());
					updateTaskInstanceQuery.executeUpdate();

					// 获得要回退的任务实例

					// Query taskInstanceQuery = hSession.createQuery(" from TaskInstance
					// ");
					// taskInstanceQuery.setParameter(1, task.getId());
					// taskInstanceQuery.setParameter(2, instance.getId());
					// List<TaskInstance> tis = taskInstanceQuery.list();

					jbpmContext.close();

				

			


			return false;
		}
		
		
		  /**
	     * 查看当前流程中的未结束的任务
	     * @param 流程ID
	     * @return
	     */
	    public Integer selectunfinishedTasksSize(Long id)
	    {
	    	Integer size = 0;
	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				 TaskMgmtInstance tmi = processInstance.getTaskMgmtInstance();  
				 
			     Collection<TaskInstance> unfinishedTasks = tmi.getUnfinishedTasks(processInstance.getRootToken());
			     size = unfinishedTasks.size();
			     return size;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return size;
	    }
	    
	    /**
	     * 查看当前流程中的未结束的任务
	     * @param 流程ID
	     * @return
	     */
	    public Collection<TaskInstance> selectunfinishedTasks(Long id)
	    {
	    	
	    	Collection<TaskInstance> unfinishedTasks = new ArrayList<TaskInstance>();;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				 TaskMgmtInstance tmi = processInstance.getTaskMgmtInstance();  
			     unfinishedTasks = tmi.getUnfinishedTasks(processInstance.getRootToken());
			     return unfinishedTasks;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return unfinishedTasks;
	    }
	    
	    /**
	     * 查看当前流程中的所有任务

	     * @param 流程ID
	     * @return
	     */
	    public Collection<TaskInstance> selectAllTasks(Long id)
	    {
	    	
	    	Collection<TaskInstance> Tasks = new ArrayList<TaskInstance>();;
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				 TaskMgmtInstance tmi = processInstance.getTaskMgmtInstance();  
				 Tasks = tmi.getTaskInstances();
				 tmi.getTaskInstances().size();
			     return Tasks;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				jbpmContext.close();
			}
			return Tasks;
	    }
	
	    /**
	     * 判断流程是否结束
	     * @param 流程ID
	     * @return
	     */
	    public Boolean isEnd(Long id)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				if(processInstance !=null)
				{
					
					return processInstance.hasEnded();
				}
				else
				{
					return false;
				}
			     
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			
	    }
	    /**
	     * 结束一个流程

	     * @param 流程ID
	     * @return
	     */
	    public Boolean processInstanceEnd(Long id)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				processInstance.end();
			    return true;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			
	    }
	    
	    /**
	     * 判断流程是否中止
	     * @param 流程ID
	     * @return
	     */
	    public Boolean isSuspend(Long id)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				if(processInstance !=null)
				{
					
					return processInstance.isSuspended();
				}
				else
				{
					return false;
				}
			     
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			
	    }
	    
	    /**
	     * 中止一个流程

	     * @param 流程ID
	     * @return
	     */
	    public Boolean processInstanceSuspend(Long id)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				processInstance.suspend();
			    
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			return true;
	    }
	    
	    /**
	     * 重启一个流程

	     * @param 流程ID
	     * @return
	     */
	    public Boolean processInstanceResume(Long id)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				processInstance.resume();
			    
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			return true;
	    }
	    
	    /**
	     * 改变任务参与者

	     * @param 任务ID
	     * @param 参与者ID
	     * @return
	     */
	    public Boolean changTaskActor(Long taskInstanceId,String actorId)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	
	    	try {
	    		TaskInstance taskInstance =	jbpmContext.loadTaskInstance(taskInstanceId);
				taskInstance.setActorId(actorId);
			//	TjsMessageIm tjsMessageIm = new TjsMessageIm();
	    	//	tjsMessageIm.sendMessage(actorId, "您有一个"+taskInstance.getProcessInstance().getProcessDefinition().getName()+"待办");
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			return true;
	    }
	    
	    /**
	     * 动态创建任务

	     * @param 任务ID
	     * @param 参与者ID
	     * @return
	     */
	    public Boolean createTaskInstance(Long taskInstanceId,String actorId)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	
	    	try {
	    		
 	    		TaskInstance taskInstance =	jbpmContext.loadTaskInstance(taskInstanceId); 
	    		jbpmContext.getProcessInstance(taskInstance.getProcessInstance().getId()).
	    		getTaskMgmtInstance().createTaskInstance(taskInstance.getTask(),taskInstance.getProcessInstance().getRootToken()).setActorId(actorId);
	    	//	TjsMessageIm tjsMessageIm = new TjsMessageIm();
	    	//	tjsMessageIm.sendMessage(actorId, "您有一个"+taskInstance.getProcessInstance().getProcessDefinition().getName()+"待办");
	    	} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			return true;
	    }
	    
	    /**
	     * 改变任务名称
	     * @param 任务ID
	     * @param 参与者ID
	     * @return
	     */
	    public Boolean changTaskName(Long taskInstanceId,String taskName)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	
	    	try {
	    		TaskInstance taskInstance =	jbpmContext.loadTaskInstance(taskInstanceId);
				taskInstance.setName(taskName);
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			finally
			{
				jbpmContext.close();
			}
			return true;
	    }
	    
	    /**
	     * 删除任务
	     * @param 任务ID
	     * @return
	     */
//	    public Boolean removeTaskInstance(Long taskInstanceId)
//	    {	    	
//	    	jbpmContext = jbpmConfiguration.createJbpmContext();
//	    	try {
//	    		Session hSession = jbpmContext.getSession();
// 	    		TaskInstance taskInstance =	jbpmContext.loadTaskInstance(taskInstanceId); 
// 	    		// 删除对应的所有日志信息//
//
//				Query deleteLogQuery = hSession
//						.createSQLQuery("delete from JBPM_LOG where 1=1 or TOKEN_ = ? or TASKINSTANCE_ = ?");
//				deleteLogQuery.setParameter(0, taskInstance.getId());
//				deleteLogQuery.setParameter(1, taskInstance.getToken().getId());
//				deleteLogQuery.executeUpdate();
//
//				// 删除任务实例
//				Query deleteTaskInstanceQuery = hSession
//						.createSQLQuery("delete from JBPM_TASKINSTANCE where ID_ = ?");
//				deleteTaskInstanceQuery.setParameter(0, taskInstance.getId());
//				deleteTaskInstanceQuery.executeUpdate();
//	    		return true;
//			} catch (RuntimeException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//				return false;
//			}
//			finally
//			{
//				jbpmContext.close();
//			}
//			
//	    }
	    
	    
	    /**
	     * 返回流程实例开始时间

	     * @param 流程ID
	     * @return
	     */
	    public Date processInstanceDate(Long id)
	    {	    	
	    	jbpmContext = jbpmConfiguration.createJbpmContext();
	    	ProcessInstance processInstance = null;
	    	Date date = new Date();
	    	try {
				processInstance = jbpmContext.getProcessInstance(id);
				date = processInstance.getStart();
			    
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			finally
			{
				jbpmContext.close();
			}
			return date ;
	    }
	    
}
